/*
 *接口编写处...
*如果接口需要做Action的权限验证，请在Action上使用属性
*如: [ApiActionPermission("big_data_inventory",Enums.ActionPermissionOptions.Search)]
 */
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Entity.DomainModels;
using VOL.JIRUI.IServices;
using Microsoft.AspNetCore.Authorization;
using System.Collections;
using System.Globalization;
using VOL.Core.Filters;
using VOL.Core.DBManager;
using System.Linq;
using VOL.Core.Extensions;

namespace VOL.JIRUI.Controllers
{
    public partial class big_data_inventoryController
    {
        private readonly Ibig_data_inventoryService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;

        [ActivatorUtilitiesConstructor]
        public big_data_inventoryController(
            Ibig_data_inventoryService service,
            IHttpContextAccessor httpContextAccessor
        )
        : base(service)
        {
            _service = service;
            _httpContextAccessor = httpContextAccessor;
        }

        /// <summary>
        /// 根据VIN去DMS系统查询对应的库存数据
        /// </summary>
        /// <returns></returns>
        ///[ApiTask]
        [HttpGet,HttpPost, Route("get_dms_inventory"), AllowAnonymous]
        ///[ApiActionPermission()]
        public dynamic get_dms_inventory()
        {
            execute_sql_dictionary execute_sql_dictionary = DBServerProvider.DbContext.Set<execute_sql_dictionary>().Where(x => (x.classify_route == "get_dms_inventory")).FirstOrDefault();
            //DMS库存信息表
            execute_sql_dictionary.sql = execute_sql_dictionary.sql.Replace("'", "''");
            List<dynamic> dditem = DBServerProvider.SqlDapper.QueryList<dynamic>("select * from openquery(DMS,'" + execute_sql_dictionary.sql + "')", "").ToList(); ;//订单明细,通过dblink取数，SQL运行的是oracle预警
            foreach (var item in dditem)
            {
                string temp = item.VIN;
                big_data_inventory big_data_inventory1 = DBServerProvider.DbContext.Set<big_data_inventory>().Where(x => x.vin == temp).FirstOrDefault();
                if (big_data_inventory1.IsNullOrEmpty())
                {
                    big_data_inventory big_data_inventory = new big_data_inventory();
                    big_data_inventory.vin = item.VIN;
                    big_data_inventory.wide_type = item.WIDE_TYPE;
                    big_data_inventory.is_move_stock = item.IS_MOVE_STOCK == null ? 0 : int.Parse(item.IS_MOVE_STOCK);
                    big_data_inventory.stock_location = item.STOCK_LOCATION;
                    big_data_inventory.first_dept_name = item.FIRST_DEPT_NAME;
                    // 库位跟库存地点相同
                    big_data_inventory.storage_bin = item.STOCK_LOCATION;
                    big_data_inventory.customer_code = item.CUSTOMER_CODE;
                    big_data_inventory.customer_name = item.CUSTOMER_NAME;
                    big_data_inventory.shipping_method = item.SHIPPING_METHOD;
                    big_data_inventory.CREATE_DATE = item.CREATE_DATE == null ? null : DateTime.ParseExact(item.CREATE_DATE, "yyyyMMddHHmmss", CultureInfo.CurrentCulture);
                    big_data_inventory.SAP_VOUCHER_TIME = item.SAP_VOUCHER_TIME == null ? null : DateTime.ParseExact(item.SAP_VOUCHER_TIME, "yyyyMMddHHmmss", CultureInfo.CurrentCulture);
                    big_data_inventory.on_transit_days = item.ON_TRANSIT_DAYS == null ? 0 : int.Parse(item.ON_TRANSIT_DAYS);
                    big_data_inventory.is_sap_valid = item.IS_SAP_VALID;
                    big_data_inventory.Creator = "DMS";
                    big_data_inventory.CreateDate = DateTime.Now;
                    DBServerProvider.DbContext.Add(big_data_inventory);
                }
                else
                {
                    big_data_inventory1.wide_type = item.WIDE_TYPE;
                    big_data_inventory1.is_move_stock = item.IS_MOVE_STOCK == null ? 0 : int.Parse(item.IS_MOVE_STOCK);
                    big_data_inventory1.stock_location = item.STOCK_LOCATION;
                    big_data_inventory1.first_dept_name = item.FIRST_DEPT_NAME;
                    // 库位跟库存地点相同
                    big_data_inventory1.storage_bin = item.STOCK_LOCATION;
                    big_data_inventory1.customer_code = item.CUSTOMER_CODE;
                    big_data_inventory1.customer_name = item.CUSTOMER_NAME;
                    big_data_inventory1.shipping_method = item.SHIPPING_METHOD;
                    big_data_inventory1.CREATE_DATE = item.CREATE_DATE == null ? null : DateTime.ParseExact(item.CREATE_DATE, "yyyyMMddHHmmss", CultureInfo.CurrentCulture);
                    big_data_inventory1.SAP_VOUCHER_TIME = item.SAP_VOUCHER_TIME == null ? null : DateTime.ParseExact(item.SAP_VOUCHER_TIME, "yyyyMMddHHmmss", CultureInfo.CurrentCulture);
                    big_data_inventory1.on_transit_days = item.ON_TRANSIT_DAYS == null ? 0 : int.Parse(item.ON_TRANSIT_DAYS);
                    big_data_inventory1.is_sap_valid = item.IS_SAP_VALID;
                    big_data_inventory1.Modifier = "DMS";
                    big_data_inventory1.ModifyDate = DateTime.Now;
                    DBServerProvider.DbContext.Update(big_data_inventory1);
                }
               
            }
            DBServerProvider.DbContext.SaveChanges();
            return dditem.Count;
        }
    }
}
